var httpUtil = require('../../utils/httpUtil.js');
// pages/me/me.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    outline: false,
    hidden: false,
    list: [],
    type: '',
    num: '',
    Reason: '',
    kefuphone: '',
    id: '',
    noData:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      type: JSON.parse(options.data).wlname,
      num: JSON.parse(options.data).wlnumber,
      id: JSON.parse(options.data).id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //获取当前网络状态
    wx.getNetworkType({
      success: function(res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        if (res.networkType == 'none' || res.networkType == 'unknown') {
          that.setData({
            outline: true
          })
        } else {
          that.setData({
            outline: false,
          })
          that.post()
        }
      }
    })

    //监听网络状态
    wx.onNetworkStatusChange(function(res) {
      if (res.networkType == 'none' || res.networkType == 'unknown') {
        that.setData({
          outline: true
        })
      } else {
        that.setData({
          outline: false,
        })
        that.post()

      }
    })
  },
  post: function() {
    var that = this;
    that.setData({
      hidden: true
    })
    var req = {
      token: wx.getStorageSync("token"),
      id: that.data.id
    }
    httpUtil.post("/crm/getwl", req)
      .then(function(res) {
        if (res.data.address.status == 200) {
          var arr = [];
          console.log(res.data.address.data.Traces)
          if (res.data.address.data.Traces.length == 0) {
            that.setData({
              Reason: '暂无轨迹信息',
              noData:true
            })
          } else {
            for (var i = 0; i < res.data.address.data.Traces.length; i++) {
              arr.push(res.data.address.data.Traces[i])
            }
            that.setData({
              noData: false,
            })
          }
          that.setData({
            list: arr,
            kefuphone: res.data.kefuphone,
            hidden: false,
          })

        } else {
          wx.showToast({
            title: '' + res.data.message + '',
          })
        }
      }).catch(function(res) {
        console.log(res);
      });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})